home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AppsToGo / Kibitz / Window.c < prev   
Encoding:
C/C++ Source or Header  |  1994-09-22  |  5.9 KB  |  232 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        window.c
  5. ** Written by:  Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved. */
  9.  
  10.  
  11.  
  12. /*****************************************************************************/
  13.  
  14.  
  15.  
  16. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  17. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  18. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  19.  
  20. #ifndef __ERRORS__
  21. #include <Errors.h>
  22. #endif
  23.  
  24. #ifndef __GWLAYERS__
  25. #include <GWLayers.h>
  26. #endif
  27.  
  28. #ifndef __LOWMEM__
  29. #include <LowMem.h>
  30. #endif
  31.  
  32. #ifndef __UTILITIES__
  33. #include <Utilities.h>
  34. #endif
  35.  
  36.  
  37.  
  38. /*****************************************************************************/
  39.  
  40.  
  41.  
  42. extern short    gPrintPage;        /* Non-zero means we are printing. */
  43. extern LayerObj    gBoardLayer;
  44.  
  45.  
  46.  
  47. /*****************************************************************************/
  48. /*****************************************************************************/
  49.  
  50. #ifdef applec
  51. #pragma segment Window
  52. #endif
  53.  
  54. /*****************************************************************************/
  55. /*****************************************************************************/
  56.  
  57.  
  58.  
  59. /* This function creates a new application window.  An application window
  60. ** contains a document which is referenced by a handle in the refCon field. */
  61.  
  62. OSErr    AppNewWindow(FileRecHndl frHndl, WindowPtr *retWindow, WindowPtr behind)
  63. {
  64.     WindowPtr    oldPort, window;
  65.     OSErr        err;
  66.     Rect        rct;
  67.  
  68.     /* We will allocate our own window storage instead of letting the Window
  69.     ** Manager do it because GetNewWindow may load in temp. resources before
  70.     ** making the NewPtr call, and this can lead to heap fragmentation. */
  71.  
  72.     GetPort(&oldPort);
  73.  
  74.     err = memFullErr;        /* Assume that we will fail.  Good attitude. */
  75.  
  76.     SetRect(&rct, 0, 0, 0, 0);
  77.     window = GetStaggeredWindow(rWindow, nil, false, FrontWindow(), behind, true, rct, (long)frHndl);
  78.     if (window) {
  79.         (*frHndl)->fileState.window = window;
  80.         AppNewWindowTitle(window);
  81.         if (!(err = AppNewWindowControls(frHndl, window, behind))) {
  82.             if (gPrintPage) MoveWindow(window, 16384, 16384, true);
  83.                 /* So the window can be hidden while printing, yet
  84.                 ** PrintMonitor can get the document name. */
  85.             if ((*frHndl)->doc.justBoardWindow)
  86.                 ZoomToWindowDevice(window, rJustBoardWindowWidth, rWindowHeight, inZoomOut, true);
  87.             ShowWindow(window);
  88.             if (gPrintPage)
  89.                 MoveWindow(window, 16384, 16384, true);
  90.                     /* Moving invisible windows to the front doesn't always
  91.                     ** get them to the front.  Now that it is visible, moving
  92.                     ** it will definitely get it to the front. */
  93.         }
  94.     }
  95.  
  96.     SetPort(oldPort);
  97.     if (retWindow) *retWindow = window;
  98.  
  99.     return(err);
  100. }
  101.  
  102.  
  103.  
  104. /*****************************************************************************/
  105.  
  106.  
  107.  
  108. /* This function updates the window title to reflect the new document name.
  109. ** The new document name is stored in the fileState portion of the document.
  110. ** This is automatically set to 'Untitled # N' for new documents, and is
  111. ** updated when a user does a save-as. */
  112.  
  113. void    AppNewWindowTitle(WindowPtr window)
  114. {
  115.     FileRecHndl    frHndl;
  116.     Str255        wTitle;
  117.  
  118.     frHndl = (FileRecHndl)GetWRefCon(window);
  119.     if (frHndl) {
  120.         pcpy(wTitle, (*frHndl)->fileState.fss.name);
  121.         SetWTitle(window, wTitle);
  122.     }
  123. }
  124.  
  125.  
  126.  
  127. /*****************************************************************************/
  128.  
  129.  
  130.  
  131. /* This function returns the state of the window's document.  If the document
  132. ** is dirty, then true is returned.  If the document is clean, or the window
  133. ** has no document, then false is returned. */
  134.  
  135. Boolean    AppWindowDirty(WindowPtr window)
  136. {
  137.     FileRecHndl    frHndl;
  138.  
  139.     frHndl = (FileRecHndl)GetWRefCon(window);
  140.     if (frHndl)
  141.         return(AppDocumentDirty(frHndl));
  142.  
  143.     return(false);
  144. }
  145.  
  146.  
  147.  
  148. /*****************************************************************************/
  149.  
  150.  
  151.  
  152. /* Close all the windows.  This is called prior to quitting the application.
  153. ** This function returns true if all windows were closed.  The user may decide
  154. ** to abort a save, thus stopping the closing of the windows.  If the user
  155. ** does this, false will be returned, indicating that all windows were not
  156. ** closed after all. */
  157.  
  158. Boolean    CloseAllWindows(void)
  159. {
  160.     WindowPtr    window;
  161.  
  162.     while ((window = (WindowPtr)LMGetWindowList()) != nil) {
  163.         /* While we have a front window, try closing it. */
  164.  
  165.         if (!CloseOneWindow(window, iQuit)) return(false);
  166.             /* When CloseOneWindow returns false, this means that the window
  167.             ** didn't close.  The only cause of this is if the window had a
  168.             ** document that needed saving, and the user cancelled the save.
  169.             ** If the window succeeded in getting closed, then we are
  170.             ** returned true.  Either way, we return the result. */
  171.     }
  172.  
  173.     return(true);
  174. }
  175.  
  176.  
  177.  
  178. /*****************************************************************************/
  179.  
  180.  
  181.  
  182. /* Closes one window.  This window may be an application window, or it may be
  183. ** a system window.  If it is an application window, it may have a document
  184. ** that needs saving. */
  185.  
  186. Boolean    CloseOneWindow(WindowPtr window, short saveMode)
  187. {
  188.     FileRecHndl    frHndl;
  189.     OSErr        err;
  190.  
  191.     if (IsAppWindow(window)) {
  192.         /* First, if the window is an application window, try saving
  193.         ** the document.  Remember that the user may cancel the save. */
  194.  
  195.         frHndl = (FileRecHndl)GetWRefCon(window);
  196.         if (frHndl) {
  197.             err = AppSaveDocument(frHndl, window, saveMode);
  198.             if (err) {
  199.                 if (err != userCanceledErr)
  200.                     Alert(rErrorAlert, gAlertFilterUPP);
  201.                 return(false);
  202.             }        /* Stop closing windows on error or user cancel. */
  203.  
  204.             SetOpponentType(frHndl, kOnePlayer);
  205.             AppDisposeDocument(frHndl);
  206.                 /* The document is saved, or the user doesn't care about
  207.                 ** that document, so dispose of the document. */
  208.         }
  209.     }
  210.     DisposeAnyWindow(window);
  211.  
  212.     return(true);
  213. }
  214.  
  215.  
  216.  
  217. /*****************************************************************************/
  218.  
  219.  
  220.  
  221. WindowPtr    SetFilePort(FileRecHndl frHndl)
  222. {
  223.     WindowPtr    oldPort;
  224.  
  225.     GetPort(&oldPort);
  226.     SetPort((*frHndl)->fileState.window);
  227.     return(oldPort);
  228. }
  229.  
  230.  
  231.  
  232.